<?php echo $this->render($this->includeDir . '/toolbar.php');?>
<?php echo $this->flashMessage();?>
<?php 
	$arrParam = $this->arrParam;	
	
	$config 		= new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
												'fileConfig'=>'product-category.ini'));
	
	$arrTooltip 	= $config->getConfig('tooltip');
	$imageConfig 	= $config->getConfig('item');
	
	$linkEnterColor			= $this->cmsUrl(array(
											'module' => 'admin',
											'controller' => 'admin-products',
											'action' => 'enter-color',
									));
	
	$name 			= $this->formText('name',$this->Item['name'],array('class'=>'width-300 bg-green2 floatLeft' ));
	$price 			= $this->formText('price',$this->Item['price'],array('class'=>'width-200 bg-green2 floatLeft', 'onkeyup' => 'this.value=addCommas(this.value);'));
	$sale_price		= $this->formText('sale_price',$this->Item['sale_price'],array('class'=>'width-200 bg-green2 floatLeft', 'onkeyup' => 'this.value=addCommas(this.value);'));
	
	$strTags        = json_decode($this->Item['tags'], true);
	$strTags        = empty($strTags)?'':implode(',', $strTags);
	$strTags        = Zendvn_Filter_Filter::filterContent($strTags, 'filter');
	$tags			= $this->formText('tags', $strTags,array('class'=>'width-200 floatLeft' ));
	
	$parentValue 	= $this->Item['cat_id']>0?$this->Item['cat_id']:1;
	$cat_id			= $this->cmsSelect('cat_id', $parentValue,array('class'=>'width-200 bg-green2 floatLeft','size'=>10), $this->slbParents);
	
	$image 			= $this->formFile('image',array('class'=>'bg-green2 floatLeft'));
	
	$no_price		= $this->formCheckbox('no_price', $this->Item['no_price'], array(), array(1,0)) . '<span style="margin-left: 18px; margin-top: -15px;display: inline-block;">Check nếu bạn chưa biết chính xác giá sản phẩm</span>';
	
	$images		= '<div id="loadPic" ></div>';
	if(!is_array($this->Item['images'])&&!empty($this->Item['images'])){
		$this->Item['images_hidden'] = Zend_Json::decode($this->Item['images']);		
	}
		
	if(isset($this->Item['images_hidden'])){
		$images    = $images.'<div id="result">';

		foreach($this->Item['images_hidden'] as $val){
			$tmp.='<div id="'.$val['name'].'" class="image"><img class="img_upload" height="150" width="150" src="' . $imageConfig['thumbUrl2'] . '/' .$val['name'].'" />';
			$tmp.=$this->formHidden('images_hidden['.$val['name'].']',$val['name'])."
			<img class='btn_delete' src='" . SCRIPTS_URL . "/uploadify/cancel.png"."' onClick='deleteImage(this)' /></div>";
		}
		$images .=$images.$tmp.'</div>';
	}

	$options 		= array('active'=>$this->translate('Active'),'inactive'=>$this->translate('Inactive'));	
	$status 		= $this->formSelect('status',$this->Item['status'],array('class'=>'width-200'),$options);
	
	$options 		= array('inactive'=>$this->translate('No'),'active'=>$this->translate('Yes'));
	$special 		= $this->formSelect('special',$this->Item['special'],array('class'=>'width-200'),$options);
	
	$options 		= array('inactive'=>$this->translate('No'),'active'=>$this->translate('Yes'));
	$home_page 		= $this->formSelect('home_page',$this->Item['home_page'],array('class'=>'width-200'),$options);
	
	$content		= Zendvn_Filter_Filter::filterContent($this->Item['content'],'filter');
	$content		= $this->tinyMce('content',$content,array('style'=>"width: 100%; height: 350px;"));
	$content		= '<div id="d_content" style="width:98%; float: left">' . $content . '</div>';
	
	$html_descript 	= $this->formTextarea('html_descript',$this->Item['html_descript'],array('class'=>'width-400 floatLeft','rows'=>8));
	$html_keyword 	= $this->formTextarea('html_keyword',$this->Item['html_keyword'],array('class'=>'width-400 floatLeft','rows'=>8));

	
	$arrTooltip = array(
						'name'=>"Name of product.",
						'price'=>"Price of product.",
						'sale_price'=>"Sale Price of product.",
						'tags'=>"Tags of product.",
						'cat_id'=>"Category of product",
						'd_content'=>"Content of product",
						'html_descript'=>"Content of meta description for SEO",
						'html_keyword'=>"Content of meta keyword for SEO",
						'image'=>"Picture of product",
						'status'=>'Status of product',
						'special'=>'Choose product is special',
						'home_page'=>'Choose product appear home page',
						);
	
	if(count($arrTooltip)>0){
		foreach ($arrTooltip as $key => $val){
			$attribs = array();
			if($key=='d_content') $attribs = array('leftOffset'=>-305);
			$formHelp  .= $this->cmsFormHelp('.form-table #' . $key, $this->translate($val), $attribs);
		}
	}
	/*=======================================================
	 * Start - Hiển thị các thông báo error của Form 
	 *=======================================================*/	
	 if(count($this->errors)>0){	
	 	foreach ($this->errors as $key => $val){	 		
	 		$$key .= $this->cmsError($key,$val);	
	 		$selector = '.form-table #' . $key . '-error';
	 		$options = array('type'=>'error','name'=>'#' .$key);
	 		$formHelp  .= $this->cmsFormHelp($selector, $this->translate($val),null,$options);
	 	}
	 }
		
	 if($this->arrParam['action'] == 'edit'){
	 	$current_image 		= '<div id="load-content-image" class="clr width-150 center">';
		if(!empty($this->Item['image'])){
			$imageUrl 		= $imageConfig['thumbUrl1'] . '/' . $this->Item['image'];
			$removeLink 	= $this->url(array('action'=>'remove-pic','type'=>'image','file'=>$this->Item['image']));
			$current_image .= '<br>' . $this->cmsImage('image',$imageUrl,array('width'=>'75px'))
							. '<br><a  class="remove" href="javascript:loadPage(\'div#load-content-image\',\'' . $removeLink . '\')">remove</a>';
		}
		
		$current_image .= $this->formHidden('current_image',$this->Item['image'])
						. '</div>';
		$image .=	$current_image;
	}
	
	$arrRow 					= array(
										array('label'=>$this->translate('Tên'),'input'=>$name),						
										array('label'=>$this->translate('Thể loại'),'input'=>$cat_id),	
										array('label'=>$this->translate('Trang chủ'),'input'=>$home_page),
										array('label'=>$this->translate('Đặc biệt'),'input'=>$special),					
										array('label'=>$this->translate('Giá'),'input'=>$price),						
										array('label'=>$this->translate('Giảm giá'),'input'=>$sale_price),		
										array('label'=>$this->translate('Giá đang cập nhật'),'input'=>$no_price),
										array('label'=>$this->translate('Tags'),'input'=>$tags),						
										array('label'=>$this->translate('Hình ảnh'),'input'=>$image),
										array('label'=>$this->translate('Hình ảnh liên quan'),'input'=>$images),
										array('label'=>$this->translate('Nội dung'),'input'=>$content),
										array('label'=>$this->translate('Mô tả Html'),'input'=>$html_descript),
										array('label'=>$this->translate('Từ khóa Html'),'input'=>$html_keyword),
										array('label'=>$this->translate('Trạng thái'),'input'=>$status),
									 );
	$tdForm 					= $this->partialLoop('row.php',$arrRow);
	
	$linkEnterStock			= $this->cmsUrl(array(
											'module' => 'admin',
											'controller' => 'admin-products',
											'action' => 'enter-stock',
											));

									
	$linkGetTags			= $this->cmsUrl(array(
											'module' => 'admin',
											'controller' => 'admin-products',
											'action' => 'get-tags'
											));
?>
<div id="element-box">
	<div class="m">
		<div class="form-table">
            	<fieldset class="admin-fieldset">
	                <legend class="admin-legend"><?php echo $this->translate('Form Nhập Liệu')?></legend>
	            	<table width="100%" border="0">
	                	<?php echo $tdForm;?>
	                </table>
	                <div class="clr"></div>
                </fieldset>
            </div>
            <div class="clr"></div>
	</div>
</div>
<script type="text/javascript">
	function addCommas(nStr){
		nStr = nStr.replace(/,/g, "");
		nStr += '';
		var x = nStr.split('.');
		var x1 = x[0];
		var x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
	$(document).ready(function() {
		$('#price, #sale_price').val(function(){
			return addCommas($(this).val());
		});
		<?php echo $formHelp;?>
		$.post("<?php echo $this->baseUrl('admin/admin-products/uploadify'); ?>",function(data){
			$('#loadPic').html(data);
		});

		$('#tags').tagsInput({
			width: 'auto',
			placeholderColor : '#ccc',
			autocomplete_url:'<?php echo $linkGetTags?>' // jquery ui autocomplete requires a json endpoint
		});
	});

	
</script>